package com.yang.crawler.web;

import com.yang.crawler.CrawlArticleCommand;
import com.yang.crawler.CrawlColumnCommand;
import com.yang.crawler.DownloadArticleCommand;
import com.yang.crawler.api.CrawlArticleService;
import com.yang.crawler.api.DownloadPdfService;
import com.yang.crawler.common.Response;
import com.yang.crawler.dto.data.ArticleDTO;
import com.yang.crawler.dto.data.ColumnDTO;
import com.yang.crawler.dto.data.DownloadDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@RestController
@RequestMapping(value = "/crawl")
public class CrawlArticleController {
    @Autowired
    private CrawlArticleService crawlArticleService;

    @Autowired
    private DownloadPdfService downloadPdfService;

    @PostMapping(value = "/crawlArticlesOfColumn")
    public Response<ColumnDTO> crawlArticlesOfColumn(@RequestBody CrawlColumnCommand crawlColumnCommand) {
        return crawlArticleService.crawlArticlesOfColumn(crawlColumnCommand);
    }

    @PostMapping(value = "/crawlArticle")
    public Response<String> crawlArticle(@RequestBody CrawlArticleCommand crawlArticleCommand) {
        Response<ArticleDTO> articleDTOResponse = crawlArticleService.crawlArticle(crawlArticleCommand);
        if (!articleDTOResponse.isSuccess()) {
            return Response.fail();
        }
        ArticleDTO articleDTO = articleDTOResponse.getData();
        if (articleDTO == null) {
            return Response.fail();
        }
        return Response.success(articleDTO.getHtmlContent());
    }


    @PostMapping(value = "/downloadArticle")
    public void downloadArticle(@RequestBody DownloadArticleCommand downloadArticleCommand, HttpServletResponse response) throws UnsupportedEncodingException {
//        System.out.println(downloadArticleCommand);
        Response<DownloadDTO> downloadDTOResponse = downloadPdfService.downloadArticle(downloadArticleCommand);
        if (downloadDTOResponse.isSuccess()) {
            DownloadDTO downloadDTO = downloadDTOResponse.getData();
            downloadFile(response, downloadDTO);
        }
    }

    private void downloadFile(HttpServletResponse response, DownloadDTO downloadDTO) throws UnsupportedEncodingException {
        String filename = downloadDTO.getFilename();
        byte[] data = downloadDTO.getData();

        response.reset();
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
        try (ServletOutputStream outputStream = response.getOutputStream();
             ByteArrayInputStream inputStream = new ByteArrayInputStream(data)) {
            byte[] buffer = new byte[8 * 1024];
            int len = -1;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
